package nguyen.example.mediaplayer;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.ContentHandler;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Environment;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class SongsManager  {

	// SDCard Path
	final String MEDIA_PATH = new String(Environment
			.getExternalStorageDirectory().getPath() + "/");
	private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

	// Constructor
	public SongsManager() {

	}

	/**
	 * Function to read all mp3 files from sdcard and store the details in
	 * ArrayList
	 * */

	public ArrayList<HashMap<String, String>> getPlayList() {
		if (MEDIA_PATH != null) {
			File home = new File(MEDIA_PATH);
			File[] listFiles = home.listFiles();
			if (listFiles != null && listFiles.length > 0) {
				for (File file : listFiles) {
					if (file.isDirectory()) {
						scanDirectory(file);
					} else {
						addSongToList(file);
					}
				}
			}
		}
		// return songs list array
		return songsList;
	}

	private void scanDirectory(File directory) {
		if (directory != null) {
			File[] listFiles = directory.listFiles();
			if (listFiles != null && listFiles.length > 0) {
				for (File file : listFiles) {
					if (file.isDirectory()) {
						scanDirectory(file);
					} else {
						addSongToList(file);
					}

				}
			}
		}
	}

	private void addSongToList(File song) {
		// if (song.getName().endsWith(mp3Pattern) )
		MediaPlayer md = new MediaPlayer();
		try {

			if ((new FileExtensionFilter().accept(song, song.getName())) == true) {

				md.setDataSource(song.getPath());
				md.prepare();
				long milliseconds = md.getDuration();
				int second = (int) ((milliseconds % (1000 * 60 * 60))
						% (1000 * 60) / 1000);
				//more than 10 second
				if (second > 10) {
					HashMap<String, String> songMap = new HashMap<String, String>();
					songMap.put(
							"songTitle",
							song.getName().substring(0,
									(song.getName().length() - 4)));
					songMap.put("songPath", song.getPath());

					// Adding each song to SongList
					songsList.add(songMap);
				}
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Class to filter files which are having .mp3 extension
	 * */
	class FileExtensionFilter implements FilenameFilter {

		private String[] audiofilter = {"mp3"};

		public boolean accept(File dir, String name) {
			for (int _i = 0; _i < audiofilter.length; _i++) {
				if (name.endsWith("." + audiofilter[_i]))
					return true;
			}

			return false;
		}
	}
}
